package com.chen.stopwatch.meizu.lap_view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.chen.stopwatch.meizu.R;
import com.chen.stopwatch.meizu.utils.Utils;

public class ColorSelectorAdapter extends BaseAdapter {
	private Context mContext = null;

	// private String[] COLORS = null;

	private int[] mColors = null;// { 0xFF0000, 0xFFA500, 0xFFFF00, 0x008000,
									// 0x0000CD,

	// 0x0000FF, 0xFF0000 };

	private LayoutInflater mInflater = null;

	private static final int COLOR_COUNT = 7;

	public ColorSelectorAdapter(Context context) {
		if (null == context)
			return;
		mContext = context;
		// COLORS = mContext.getResources().getStringArray(R.array.colors);
		// mColors = mContext.getResources().getIntArray(R.array.color_value);
		mColors = new int[COLOR_COUNT];

		mColors[0] = mContext.getResources().getColor(R.color.red);
		mColors[1] = mContext.getResources().getColor(R.color.orange);
		mColors[2] = mContext.getResources().getColor(R.color.yellow);
		mColors[3] = mContext.getResources().getColor(R.color.green);
		mColors[4] = mContext.getResources().getColor(R.color.aqua);
		mColors[5] = mContext.getResources().getColor(R.color.blue);
		mColors[6] = mContext.getResources().getColor(R.color.purple);

		// outputColor();
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getColor(int position) {
		if (null == mColors)
			return -1;
		if (position >= 0 && position < COLOR_COUNT)
			return mColors[position];
		return -1;
	}

	public void outputColor() {
		if (null == mColors)
			return;
		for (int color : mColors) {
			Utils.logDebug("test", "color is = " + color);
		}
	}

	public void destroy() {
		mContext = null;
		mColors = null;
		mInflater = null;
	}

	public int getCount() {
		if (null == mColors)
			return 0;
		return mColors.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == mInflater || null == mColors)
			return convertView;
		if (null == convertView) {
			convertView = mInflater.inflate(R.layout.color_selector_item, null);
			if (null == convertView)
				return convertView;
		}

		convertView.setBackgroundDrawable(null);
		convertView.setBackgroundColor(mColors[position]);
		return convertView;
	}

}
